package com.realtyManage.springboot.Service.impl;

import com.realtyManage.springboot.Dao.ArticleRepository;
import com.realtyManage.springboot.Model.Do.Article;
import com.realtyManage.springboot.Service.IActiveService;
import com.realtyManage.springboot.Service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * ArticleServiceImpl
 *
 * @Author 杭州素色网络科技有限公司
 * @Date 2019/5/15
 **/
@Service
@Transactional
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleRepository articleRepository;
    @Autowired
    private IActiveService activeService;

    @Override
    public Page<Article> findPagedByTypeAndState(Integer type, int state, Integer pageNumber, Integer pageSize) {
        Sort sort = new Sort(Sort.Direction.DESC,"id");
        Pageable pageable =new PageRequest(pageNumber - 1, pageSize, sort);
        if(type == 2 || type == 3){
            List<Integer> types = new ArrayList<Integer>(){
                {add(2); add(3);}
            };
            Page<Article> articlePage = articleRepository.findByStateAndTypeIn(pageable,state,types);
            for(Article article : articlePage.getContent()){
                article.setCustNum(activeService.countByArtId(article.getId()));
            }
            return articlePage;
        }else{
            return articleRepository.findByTypeAndState(pageable,type,state);
        }
    }

    @Override
    public Article getOne(Integer artId) {
        Article article = articleRepository.getOne(artId);
        if(article==null || article.getState()!=1){
            return null;
        }else{
            return  article;
        }
    }

    @Override
    public int save(Article article) {
        if(article.getId() != null && article.getId() != ' '){
            return articleRepository.update(article);
        }else{
            return articleRepository.save(article).getId();
        }

    }

    @Override
    public int update(Article article) {
        return articleRepository.update(article);
    }

    @Override
    public List<Article> findByEndTimeAndRegion(int artId) {
        return articleRepository.findByEndTimeAndRegion(artId);
    }

    /**
     * 网页版
     * @param type
     * @param state
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public Page<Article> findPagedByTypeAndStateWy(Integer type, int state, Integer pageNumber, Integer pageSize) {
        Sort sort = new Sort(Sort.Direction.DESC,"id");
        Pageable pageable =new PageRequest(pageNumber - 1, pageSize, sort);
        if(type == null){
            List<Integer> types = new ArrayList<Integer>(){
                {add(1);add(2);add(3); add(4);}
            };
            Page<Article> articlePage = articleRepository.findByStateAndTypeIn(pageable,state,types);
            for(Article article : articlePage.getContent()){
                article.setCustNum(activeService.countByArtId(article.getId()));
            }
            return articlePage;
        }else{
            return articleRepository.findByTypeAndState(pageable,type,state);
        }
    }

    @Override
    public List<Article> getHomePage() {
        return articleRepository.getHomePage();
    }
}
